package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.redis.util.RedisKeys;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
    public static String getToken(HttpServletRequest request, HttpServletResponse response) {
        String token = null;
        //获取浏览器的传过来的cookie
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    //如果有,设置路径是加时间返回给浏览器
                    cookie.setPath("/");
                    cookie.setMaxAge(RedisKeys.USER_LOGIN.getTime().intValue());

                    response.addCookie(cookie);
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}
